package articles

import (
	"blog/app/model/articles"
	"blog/app/services/system"
	Core "blog/core"
	"errors"
)

func (receiver *Tag) GetLists() ([]articles.Tag, error) {
	var lists []articles.Tag
	err := Core.DbConnect.Model(receiver.ArticleTagModel).Find(&lists).Error
	return lists, err
}

func (receiver *Tag) GetInfo(id int) (*articles.Tag, error) {
	var info articles.Tag
	var count int64
	if Core.DbConnect.Model(receiver.ArticleTagModel).Scopes(system.Condition.ArticleTag.IdFind(id)).Count(&count).Find(&info); count == 0 {
		return nil, errors.New("标签不存在")
	}

	return &info, nil
}

func (receiver *Tag) Create(param articles.Tag) error {
	return Core.DbConnect.Create(&param).Error
}

func (receiver *Tag) Edit(id int, param articles.Tag) error {
	return Core.DbConnect.Model(receiver.ArticleTagModel).Scopes(system.Condition.ArticleTag.IdFind(id)).Updates(&param).Error
}

func (receiver *Tag) Del(id int) error {
	var count int64
	db := Core.DbConnect.Model(receiver.ArticleTagModel).Scopes(system.Condition.ArticleTag.IdFind(id))
	if db.Count(&count); count == 0 {
		return errors.New("标签不存在")
	}

	return db.Delete(&articles.Tag{}).Error
}
